<template>
  <div>
    <div class="box-title">
      <button class="btn btn-success" @click="doAssign()">分配许可</button>
    </div>
    <el-tree
      :data="data2"
      show-checkbox
      :default-expanded-keys="[1,3]"
      :default-checked-keys="checkedkeys"
      node-key="id"
      ref="tree"
      highlight-current
      :props="defaultProps">
    </el-tree>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  props: {
    id: Number // 父组件传递过来的值
  },
  data () {
    return {
      data2: [],
      defaultProps: {
        children: 'children',
        label: 'name'

      },
      expandedkeys: [],
      checkedkeys: []
    }
  },
  mounted () {
    axios.post('api/role/loadData?roleid=' + this.id).then((response) => {
      this.data2 = response.data.data.data
      console.log(response.data.data.checkedList)
      this.checkedkeys = response.data.data.checkedList
    }).catch(function (error) {
      console.log(error)
    })
  },
  methods: {
    doAssign () {
      let ids = this.$refs.tree.getCheckedKeys()
      let permissionids = ''
      let jsondata = ''
      ids.forEach(item => {
        permissionids = permissionids + 'permissionids=' + item + '&'
      })
      permissionids = permissionids.substring(0, permissionids.length - 1)
      jsondata = 'roleid=' + this.id + '&' + permissionids
      axios.post('/api/role/doAssign', jsondata).then((response) => {
      }).catch(function (error) {
        console.log(error)
      })
    }
  }
}
</script>
